package com.aaa.mks.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.mks.dao.AdvertiseDao;
import com.aaa.mks.entity.Advertise;
import com.aaa.mks.service.AdvertiseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 广告位(Advertise)表服务实现类
 *
 * @author makejava
 * @since 2020-11-26 21:09:39
 */
@Service("advertiseService")
public class AdvertiseServiceImpl implements AdvertiseService {
    @Resource
    private AdvertiseDao advertiseDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Long id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.advertiseDao.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Advertise> queryAllByLimit(int offset, int limit) {
        return this.advertiseDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param advertise 实例对象
     * @return 实例对象
     */
    @Override
    public Advertise insert(Advertise advertise) {
        this.advertiseDao.insert(advertise);
        return advertise;
    }

    /**
     * 修改数据
     *
     * @param advertise 实例对象
     * @return 实例对象
     */
    @Override
    public Advertise update(Advertise advertise) {
        this.advertiseDao.update(advertise);
        return (Advertise) this.queryById(advertise.getId()).getData();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Long id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.advertiseDao.deleteById(id));
    }

    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, Advertise advertise) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Advertise> pageInfo = new PageInfo<Advertise>(advertiseDao.queryAll(advertise));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    @Override
    public Result insertOrUpdate(Advertise advertise) {
        int i = 0;
        if (advertise.getId() != null && advertise.getId() !=0) {
            i = advertiseDao.update(advertise);
            if (i>0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"修改成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"修改失败");
            }
        }else {
            i = advertiseDao.insert(advertise);
            if (i >0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"添加成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"添加失败");
            }
        }
    }
}